﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data;
using Pub;

namespace LZQXT.Test.Forms.DB
{
    public partial class FDBInit : Form
    {

        public static string S_Con;

        int i_ID;
        public string DBCon;
        public string access_con;
        public SQLiteCommand cm;
        public SQLiteConnection cn;
        //public static List<SqlConnection> lst_cn = new List<SqlConnection>();
        //public static List<string> lstConKey = new List<string>();
        //public static List<string> lstConVal = new List<string>();

        public SQLiteDataAdapter da;
        public DataSet ds;
        public int ds_count = 0;
        public SQLiteParameter[] pars;
        public List<SQLiteParameter> pars2;
        public string sqlServer_con;
        public string s_sql = "";

        public FDBInit()
        {
            InitializeComponent();
        }

        public DataSet sel1(string sql)
        {
            try
            {
                this.s_sql = sql;
                cn.Open();
                da = new SQLiteDataAdapter(sql, cn);
                ds = new DataSet();

                da.Fill(ds);
                SQLiteCommandBuilder sqlCb = new SQLiteCommandBuilder(da);

                cn.Close();

                //da.FillSchema(ds, SchemaType.Mapped);
                try
                {
                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        ds_count = 0;
                    }

                    ds_count = ds.Tables[0].Rows.Count;
                }
                catch (System.Exception ex1)
                {
                    ds_count = 0;
                    CDebug.Send("返回结果个数出错", ex1);
                }

                return ds;

            }
            catch (Exception ex)
            {
                if (cn.State != System.Data.ConnectionState.Closed) cn.Close();

                CDebug.Send(sql + "; " + ex.ToString());
                CDebug.Send("sql语句错误", ex);
                ds_count = 0;
                return null;
            }
        }


        public int cmd1(string sql)
        {
            cn.Open();
            cm = new SQLiteCommand();
            cm.Connection = cn;

            cm = new SQLiteCommand(sql, cn);

            int i = 0;
            try
            {
                i = cm.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            cn.Close();
            return i;

        }

        private void FDBInit_Load(object sender, EventArgs e)
        {
            lstBox_ConName.Dock = DockStyle.Fill;
            panel2.Dock = DockStyle.Fill;
            tbox_ConStr.Dock = DockStyle.Fill;

            db_init();
        }

        public void db_init()
        {
            DBCon = "data source=" + Application.StartupPath + "\\DBCon.db";
            cn = new SQLiteConnection(DBCon);

            DataSet ds1 = sel1("SELECT DISTINCT(ID_Main) AS C1 FROM T_DBCon");
            if (!CDB.dsIsNull(ds1))
            {
                lstBox_Server.DataSource = ds1.Tables[0];
                lstBox_Server.DisplayMember = "C1";
            }
        }

        private void FDBInit_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        /// <summary>
        /// 选择连接类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstBox_Server_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "SELECT ID, ConName, ConStr FROM T_DBCon WHERE ID_Main='" + lstBox_Server.Text + "'";
                DataSet ds1 = sel1(strSQL);
                if (!CDB.dsIsNull(ds1))
                {
                    lstBox_ConName.DataSource = ds1.Tables[0];
                    lstBox_ConName.DisplayMember = "ConName";
                    lstBox_ConName.ValueMember = "ID";

                    /*
                    ADOTest.dicConStr.Clear();
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        ADOTest.dicConStr.Add(dr1["ConName"].ToString(), dr1["ConStr"].ToString());
                    }
                    */
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        private void lstBox_ConName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSQL = "SELECT * FROM T_DBCon WHERE ID_Main='" + lstBox_Server.Text + "' AND ConName='" + lstBox_ConName.Text + "'";
            DataSet ds1 = sel1(strSQL);
            if (!CDB.dsIsNull(ds1))
            {
                tbox_ConStr.Text = ds1.Tables[0].Rows[0]["ConStr"].ToString();
                Program.fdbinit_add.s_ID = ds1.Tables[0].Rows[0]["ID"].ToString();
                Program.fdbinit_add.tboxTyp.Text = lstBox_Server.Text;
                Program.fdbinit_add.tboxConName.Text = lstBox_ConName.Text;
                Program.fdbinit_add.tboxConStr.Text = tbox_ConStr.Text;
                Program.fdbinit_add.tboxServer.Text = ds1.Tables[0].Rows[0]["Server"].ToString();
                Program.fdbinit_add.tboxUser.Text = ds1.Tables[0].Rows[0]["User"].ToString();
                Program.fdbinit_add.tboxPass.Text = ds1.Tables[0].Rows[0]["Pass"].ToString();
                Program.fdbinit_add.tboxDB.Text = ds1.Tables[0].Rows[0]["DataBase"].ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Program.fdbinit_add.i_Typ = 1;
            Program.fdbinit_add.ShowDialog();
        }

        private void btnEdt_Click(object sender, EventArgs e)
        {
            Program.fdbinit_add.i_Typ = 2;
            Program.fdbinit_add.ShowDialog();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string strSQL = "DELETE FROM T_DBCon WHERE ID = " + Program.fdbinit_add.s_ID;
            if (CPub.MsgBox(strSQL, "删除数据"))
            {
                cmd1(strSQL);
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            db_init();
        }


    }
}
